package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.webservice.SoapClient;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.XML;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.domain.HUAWEI.*;
import com.ruoyi.system.domain.InterfaceLog;
import com.ruoyi.system.domain.function.ProdOrderDTO;
import com.ruoyi.system.mapper.HUAWEI.*;
import com.ruoyi.system.mapper.InterfaceLogMapper;
import com.ruoyi.system.utils.huaweiInterfaceUtil.HuaweiInterfaceHttps;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.service.ITBtpMtlCurriculumViateTiService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * EMS厂履历数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-08
 */
@Service
public class TBtpMtlCurriculumViateTiServiceImpl extends ServiceImpl<TBtpMtlCurriculumViateTiMapper, TBtpMtlCurriculumViateTi> implements ITBtpMtlCurriculumViateTiService {
    @Autowired
    private InterfaceLogMapper interfaceLogMapper;
    @Autowired
    private EmsBarcodeShipmentMapper emsBarcodeShipmentMapper;
    @Autowired
    private SubmitTaskNoInfoMapper submitTaskNoInfoMapper;
    @Autowired
    private EmsBarcodeAttributeMapper emsBarcodeAttributeMapper;
    @Autowired
    private EmsBarcodeRelationMapper emsBarcodeRelationMapper;

    @Override
    public String purchaseOrder(List<TBtpMtlCurriculumViateTiDTO> list) {
        if(IterUtil.isEmpty(list)){
            throw new CustomException("采购订单同步数据为空");
        }

        //日志记录
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("WMS->MOM");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(list));
        interfaceLog.setEndTime(new Date());
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLog.setInterfaceName("采购订单同步数据");

        list.stream().forEach(i->{
            TBtpMtlCurriculumViateTi tBtp = new TBtpMtlCurriculumViateTi();
            BeanUtil.copyProperties(i,tBtp);
            tBtp.setUploadFlag(0);//默认为0 未上载 华为标识
            tBtp.setSegment40("40033");
            baseMapper.insert(tBtp);
        });
        interfaceLog.setInterfaceResponse("WMS订单同步接收成功");
        interfaceLogMapper.insert(interfaceLog);
        return "同步成功";
    }

    @Override
    public String testHuaweiZsxx(EmsBarcodeRelation emsBarcodeRelation){

        // 新建客户端(测试)
//        SoapClient client = SoapClient.create("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx")
//                // 设置要请求的方法，此接口方法前缀为web，传入对应的命名空间
//                .setMethod("web:getCountryCityByIp", "http://WebXml.com.cn/")
//                // 设置参数，此处自动添加方法的前缀：web
//                .setParam("theIpAddress", "218.21.240.106");
//
//        // 发送请求，参数true表示返回一个格式化后的XML内容
//        // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
//        String send = client.send(true);
//
//        JSONObject object = XML.toJSONObject(send);
//        Map map = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class);
//        return JSONUtil.toJsonStr(object);


        String xml ="<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<BarcodeRelationSend>\n" +
                "<Import>\n" +
                "<PO>"+emsBarcodeRelation.getPo()+"</PO>\n" +
                "<VendorID>"+emsBarcodeRelation.getVendorId()+"</VendorID>\n" +
                "<ORGID>"+emsBarcodeRelation.getOrgId()+"</ORGID>\n" +
                "<ItemCode>"+emsBarcodeRelation.getItemCode()+"</ItemCode>\n" +
                "<Barcode>"+emsBarcodeRelation.getBarCode()+"</Barcode>\n" +
                "<SonBarcode>"+emsBarcodeRelation.getSonBarCode()+"</SonBarcode>\n" +
                "<SonItemCode>"+emsBarcodeRelation.getSonItemCode()+"</SonItemCode>\n" +
                "<Quantity>"+emsBarcodeRelation.getQuantity()+"</Quantity>\n" +
                "<Supplier></Supplier>\n" +
                "<Manufactory></Manufactory>\n" +
                "<UploadFlag>0</UploadFlag>\n" +
                "<CheckMessage></CheckMessage>\n" +
                "<CreatedBy>LD001</CreatedBy>\n" +
                "<CreatedDate>"+DateUtil.format(emsBarcodeRelation.getCreatedDate(),"yyyy-MM-dd HH:mm:ss")+"</CreatedDate>\n" +
                "<UpdatedBy></UpdatedBy>\n" +
                "<UpdatedDate></UpdatedDate>\n" +
                "<Segment1>N</Segment1>\n" +
                "<Segment2>N</Segment2>\n" +
                "<Segment3>"+emsBarcodeRelation.getSegment3()+"</Segment3>\n" +
                "<Segment4></Segment4>\n" +
                "<Segment5></Segment5>\n" +
                "<Segment6></Segment6>\n" +
                "<Segment7></Segment7>\n" +
                "<ItemID></ItemID>\n" +
                "<SonItemID></SonItemID>\n" +
                "<CheckFlag>0</CheckFlag>\n" +
                "<EmsTransID>"+emsBarcodeRelation.getEmsTransId()+"</EmsTransID>\n" +
                "<CheckInfo></CheckInfo>\n" +
                "<Segment8></Segment8>\n" +
                "<Segment9></Segment9>\n" +
                "<Segment10>"+emsBarcodeRelation.getSegment10()+"</Segment10>\n" +
                "<Segment11>"+emsBarcodeRelation.getSegment11()+"</Segment11>\n" +
                "<Segment12></Segment12>\n" +
                "<Segment13></Segment13>\n" +
                "<Segment14>1</Segment14>\n" +
                "<Segment15>EMS</Segment15>\n" +
                "</Import>\n" +
                "</BarcodeRelationSend>]]>";


        try{
            //改为https方式传输数据20250415
//            String date = DateUtil.format(new Date(), "yyyyMMdd'T'HHmmss'Z'");
//            BarcodeRelationSendDTO barcodeRelationSendDTO = new BarcodeRelationSendDTO(emsBarcodeRelation);
//            String res = HuaweiInterfaceHttps.sendHuawei(HuaweiInterfaceHttps.serviceUriBarcodeRelationSend,HuaweiInterfaceHttps.http_medhod_BarcodeRelationSend,
//                            JSONUtil.toJsonStr(barcodeRelationSendDTO),date);

            SoapClient client = SoapClient.create("http://cmes.huawei.com/hw_manufacture/MidLayer.asmx")
                    .setMethod("Get_Info_Frmbarcode", "http://Auto.huawei.com.cn/")
                    .setParam("sTaskType", "BarcodeRelationSend")
                    .setParam("sImport",xml);

            // 发送请求，参数true表示返回一个格式化后的XML内容
            // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
            String send = client.send(true);
            JSONObject object = XML.toJSONObject(send);
            Map map3 = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class);
            String s = JSONUtil.toJsonStr(map3);

            //成功之后将数据存入表中
            emsBarcodeRelationMapper.insert(emsBarcodeRelation);
            return s;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "请求成功";
    }

    @Override
    @Transactional
    public String retrieval(List<EmsBarcodeShipmentDTO> list) {
        if(IterUtil.isEmpty(list)){
            throw new CustomException("成品出库信息为空！");
        }
        list.stream().forEach(i->{
            EmsBarcodeShipment emsBarcodeShipment = new EmsBarcodeShipment();
            BeanUtil.copyProperties(i,emsBarcodeShipment);

            //华为改动将Segment3与Segment18赋值一样
            emsBarcodeShipment.setSegment3(i.getSegment18());
            emsBarcodeShipment.setSegment28("40033");
            emsBarcodeShipmentMapper.insert(emsBarcodeShipment);
        });
        //日志记录
        InterfaceLog interfaceLog = new InterfaceLog();
        interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
        interfaceLog.setStartTime(new Date());
        interfaceLog.setInterfaceDirection("WMS->MOM");
        interfaceLog.setPacketSize("");
        interfaceLog.setInterfaceRequest(JSONUtil.toJsonStr(list));
        interfaceLog.setEndTime(new Date());
        interfaceLog.setInterfaceResponse("WMS成品出库接收成功");
        long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
        interfaceLog.setTimeConsuming(String.valueOf(be));
        interfaceLog.setCreateTime(new Date());
        interfaceLog.setInterfaceType("http");
        interfaceLog.setInterfaceName("WMS成品出库");
        interfaceLogMapper.insert(interfaceLog);
        return "调用成功";
    }

    @Override
    public String testHuaweiTmsx(EmsBarcodeAttribute bute) {
        String xml ="<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<SubmitBarcodeReleation>\n" +
                "<Import>\n" +
                "<EmsTransID>+"+bute.getEmsTransId()+"+</EmsTransID>\n" +
                "<OrganizationID>"+bute.getOrganizationId()+"</OrganizationID>\n" +
                "<PO>"+bute.getPo()+"</PO>\n" +
                "<ItemNumber>"+bute.getItemNumber()+"</ItemNumber>\n" +
                "<ItemVer></ItemVer>\n" +
                "<EmsCode>"+bute.getEmsCode()+"</EmsCode>\n" +
                "<VendorId>"+bute.getVendorId()+"</VendorId>\n" +
                "<Sn>"+bute.getSn()+"</Sn>\n" +
                "<Barcode2d></Barcode2d>\n" +
                "<Coo>"+bute.getCoo()+"</Coo>\n" +
                "<Updated_by></Updated_by>\n" +
                "<Updated_date>"+DateUtil.format(bute.getUpdatedDate(),"yyyy-MM-dd HH:mm:ss")+"</Updated_date>\n" +
                "<Created_by>"+bute.getCreatedBy()+"</Created_by>\n" +
                "<Created_date>"+DateUtil.format(bute.getCreatedDate(),"yyyy-MM-dd HH:mm:ss")+"</Created_date>\n" +
                "<Attribute1></Attribute1>\n" +
                "<Attribute2></Attribute2>\n" +
                "<Attribute3></Attribute3>\n" +
                "<Attribute4></Attribute4>\n" +
                "<Attribute5>"+bute.getAttribute5()+"</Attribute5>\n" +
                "<Segment1>"+bute.getSegment1()+"</Segment1>\n" +
                "<Segment2>"+bute.getSegment2()+"</Segment2>\n" +
                "<Segment3></Segment3>\n" +
                "<Segment4></Segment4>\n" +
                "<Segment5></Segment5>\n" +
                "<Type></Type>\n" +
                "<RoshFlag>"+bute.getRoshFlag()+"</RoshFlag>\n" +
                "</Import>\n" +
                "</SubmitBarcodeReleation>]]>";


        try{
            //改为https方式传输数据20250415
//            String date = DateUtil.format(new Date(), "yyyyMMdd'T'HHmmss'Z'");
//            SubmitBarcodeReleationDTO submitBarcodeReleationDTO = new SubmitBarcodeReleationDTO(bute);
//            String res = HuaweiInterfaceHttps.sendHuawei(HuaweiInterfaceHttps.serviceUriSubmitBarcodeReleation,HuaweiInterfaceHttps.http_medhod_SubmitBarcodeReleation,
//                            JSONUtil.toJsonStr(submitBarcodeReleationDTO),date);

            SoapClient client = SoapClient.create("http://cmes.huawei.com/hw_manufacture/MidLayer.asmx")
                    .setMethod("Get_Info_Frmbarcode", "http://Auto.huawei.com.cn/")
                    .setParam("sTaskType", "SubmitBarcodeReleation")
                    .setParam("sImport",xml);

            // 发送请求，参数true表示返回一个格式化后的XML内容
            // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
            String send = client.send(true);
            JSONObject object = XML.toJSONObject(send);
            Map map3 = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class);

            //成功之后存入表里面
            emsBarcodeAttributeMapper.insert(bute);

            return JSONUtil.toJsonStr(map3);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "请求成功";
    }

    @Override
    public AjaxResult getEntityRevision(String po) {

//        String xml ="<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
//                "<GetEntityRevision>\n" +
//                "<Import>\n" +
//                "<EntityName>"+po+"</EntityName>\n" +
//                "</Import>\n" +
//                "</GetEntityRevision>]]>";

        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">   \n" +
                "<soap:Body>     \n" +
                "<Get_Info_Frmbarcode xmlns=\"http://Auto.huawei.com.cn/\">       \n" +
                "<sTaskType>GetEntityRevision</sTaskType>       \n" +
                "<sImport><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" +
                "<GetEntityRevision>\n" +
                "  <Import>\n" +
                "    <EntityName>"+po+"</EntityName>\n" +
                "  </Import>\n" +
                "</GetEntityRevision>]]>\n" +
                "</sImport>     \n" +
                "</Get_Info_Frmbarcode>   \n" +
                "</soap:Body> \n" +
                "</soap:Envelope>";
        String date = DateUtil.format(new Date(), "yyyyMMdd'T'HHmmss'Z'");
        try{
            //改为https方式传输数据20250415
            String send = HuaweiInterfaceHttps.sendHuawei(HuaweiInterfaceHttps.serviceUriSoap,HuaweiInterfaceHttps.http_medhod_soap,xml,date);
//            SoapClient client = SoapClient.create("http://cmes.huawei.com/hw_manufacture/Midlayer.asmx")
//                    .setMethod("Get_Info_Frmbarcode", "http://Auto.huawei.com.cn/")
//                    .setParam("sTaskType", "GetEntityRevision")
//                    .setParam("sImport",xml);
//
//            // 发送请求，参数true表示返回一个格式化后的XML内容
//            // 返回内容为XML字符串，可以配合XmlUtil解析这个响应 todo 此方法暂时注释测试完放开
//            String send = client.send(true);
//            String send = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\r\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n  <soap:Body>\r\n    <Get_Info_FrmbarcodeResponse xmlns=\"http://Auto.huawei.com.cn/\">\r\n      <sExport>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;!--Current Version:V03R15--&gt;&lt;GetEntityRevision&gt;&lt;Export&gt;&lt;ResultData&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05018EKV&lt;/SoftItem&gt;&lt;SoftName&gt;ONTPLUGINXML888&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC150A2205140037&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;1&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;50085334&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05018EKN&lt;/SoftItem&gt;&lt;SoftName&gt;HG8021PreconfigVA60&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC160A2206270076&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;1&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;50085334&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05024FWK&lt;/SoftItem&gt;&lt;SoftName&gt;HG8021VA34&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC166&lt;/Revision&gt;&lt;SoftLocation&gt;U1001&lt;/SoftLocation&gt;&lt;ModeProcduction&gt;0&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;2&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;0302027496&lt;/Segment4&gt;&lt;Segment5&gt;40060909&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05022TEM&lt;/SoftItem&gt;&lt;SoftName&gt;ONTXML217&lt;/SoftName&gt;&lt;Revision&gt;V200R019C30B040&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;2&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;0302027496&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05018UXR&lt;/SoftItem&gt;&lt;SoftName&gt;HG8021APPVA61&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC150&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;1&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;50085334&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05018NKK&lt;/SoftItem&gt;&lt;SoftName&gt;HG8021VAEfuse27&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC125&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;1&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;50085334&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;/ResultData&gt;&lt;Message&gt;&lt;ErrorCode&gt;0&lt;/ErrorCode&gt;&lt;ErrorMsg&gt;Success&lt;/ErrorMsg&gt;&lt;/Message&gt;&lt;/Export&gt;&lt;/GetEntityRevision&gt;</sExport>\r\n    </Get_Info_FrmbarcodeResponse>\r\n  </soap:Body>\r\n</soap:Envelope>\r\n";
            JSONObject object = XML.toJSONObject(send);
            Map map3 = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class);
            Map map4 = (Map) map3.get("soap:Envelope");
            Map map5 = (Map) map4.get("soap:Body");
            Map map6 = (Map) map5.get("Get_Info_FrmbarcodeResponse");
            String sExport = (String) map6.get("sExport");

            //将给定的String文本解析为XML文档并返回新创建的document
            org.dom4j.Document document = DocumentHelper.parseText(sExport);
            //获取根节点,在例子中就是responsedata节点
            Element rootElement = document.getRootElement();
            //获取根节点下的某个元素
            Element Export = rootElement.element("Export");
            Element ResultData = Export.element("ResultData");

            Element Message = Export.element("Message");
            Element ErrorCode = Message.element("ErrorCode");
            Element ErrorMsg = Message.element("ErrorMsg");
            if("0".equals((String) ErrorCode.getData())){
                List<EntityRevisionDTO> list = new ArrayList<>();
                //遍历某个子节点，如resultcode
                for (Iterator i = ResultData.elementIterator("LineRow"); i.hasNext(); ){
                    EntityRevisionDTO entityRevision = new EntityRevisionDTO();
                    Element  next = (Element) i.next();
                    entityRevision.setZorgid((String) next.element("Zorgid").getData());
                    entityRevision.setEntityName((String) next.element("EntityName").getData());
                    entityRevision.setWorkNumber((String) next.element("WorkNumber").getData());
                    entityRevision.setItemNumber((String) next.element("ItemNumber").getData());
                    entityRevision.setItemVer((String) next.element("ItemVer").getData());
                    entityRevision.setSoftItem((String) next.element("SoftItem").getData());
                    entityRevision.setSoftName((String) next.element("SoftName").getData());
                    entityRevision.setRevision((String) next.element("Revision").getData());
                    entityRevision.setSoftLocation((String) next.element("SoftLocation").getData());
                    entityRevision.setModeProcduction((String) next.element("ModeProcduction").getData());
                    entityRevision.setSpecialRevision((String) next.element("SpecialRevision").getData());
                    entityRevision.setSoftLevel((String) next.element("SoftLevel").getData());
                    entityRevision.setExistUpdated((String) next.element("ExistUpdated").getData());
                    entityRevision.setCreateBy((String) next.element("CreateBy").getData());
                    entityRevision.setCreateDate((String) next.element("CreateDate").getData());
                    entityRevision.setUpdateBy((String) next.element("UpdateBy").getData());
                    entityRevision.setUpdateDate((String) next.element("UpdateDate").getData());
                    entityRevision.setSegment1((String) next.element("Segment1").getData());
                    entityRevision.setSegment2((String) next.element("Segment2").getData());
                    entityRevision.setSegment3((String) next.element("Segment3").getData());
                    entityRevision.setSegment4((String) next.element("Segment4").getData());
                    entityRevision.setSegment5((String) next.element("Segment5").getData());
                    list.add(entityRevision);
                }
                return AjaxResult.success(list);
            }else {
                return AjaxResult.error((String) ErrorMsg.getData());
            }

        }catch (Exception e){
//            e.printStackTrace();
            throw new CustomException("查询任务令版本信息失败："+e.getMessage());
        }

    }

    @Override
    public String uploadKg(GdkgInfoDTO dto) {
        String startTime = DateUtil.format(DateUtil.offsetHour(new Date(), 1), "yyyy/MM/dd HH:mm:ss");
        String xml ="<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<SubmitTaskNoInfo>\n" +
                "<Import>\n" +
                "<EntityName>"+dto.getEntityName()+"</EntityName>\n" +
                "<WorkNumber>"+dto.getEntityName()+"</WorkNumber>\n" +
                "<StartTime>"+dto.getStartTime()+"</StartTime>\n" +
                "<ShipmentTime></ShipmentTime>\n" +
                "<Segment1></Segment1>\n" +
                "<Segment2></Segment2>\n" +
                "<Segment3></Segment3>\n" +
                "<Segment4></Segment4>\n" +
                "<Segment5></Segment5>\n" +
                "</Import>\n" +
                "</SubmitTaskNoInfo>]]>";
        //改为https传输回传20250415


        try {
            SoapClient client = SoapClient.create("http://cmes.huawei.com/hw_manufacture/Midlayer.asmx")
                    .setMethod("Get_Info_Frmbarcode", "http://Auto.huawei.com.cn/")
                    .setParam("sTaskType", "SubmitTaskNoInfo")
                    .setParam("sImport", xml);

            // 发送请求，参数true表示返回一个格式化后的XML内容
            // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
            String send = client.send(true);
            JSONObject object = XML.toJSONObject(send);
            Map map3 = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class);
            Software software = new Software();
            software.setRes(JSONUtil.toJsonStr(map3));
            //回传华为工单开工时间日志记录
            SubmitTaskNoInfo submitTaskNoInfo = new SubmitTaskNoInfo();
            submitTaskNoInfo.setEntityName(dto.getEntityName());
            submitTaskNoInfo.setWorkNumber(dto.getWorkNumber());
            submitTaskNoInfo.setStartTime(startTime);
            submitTaskNoInfo.setRes(JSONUtil.toJsonStr(map3));
            submitTaskNoInfoMapper.insert(submitTaskNoInfo);
            return JSONUtil.toJsonStr(map3);
        }catch (Exception e){
            //异常日志记录
            InterfaceLog interfaceLog = new InterfaceLog();
            interfaceLog.setSerialNumber(String.valueOf(new Date().getTime()));
            interfaceLog.setStartTime(new Date());
            interfaceLog.setInterfaceDirection("WMS->MOM");
            interfaceLog.setPacketSize("");
            interfaceLog.setInterfaceRequest(dto.getEntityName()+";"+dto.getWorkNumber()+";"+dto.getStartTime());
            interfaceLog.setEndTime(new Date());
            interfaceLog.setInterfaceResponse(e.getMessage());
            long be = DateUtil.between(interfaceLog.getStartTime(), interfaceLog.getEndTime(), DateUnit.MS);
            interfaceLog.setTimeConsuming(String.valueOf(be));
            interfaceLog.setCreateTime(new Date());
            interfaceLog.setInterfaceType("http");
            interfaceLog.setInterfaceName("华为工单开工时间");
            interfaceLogMapper.insert(interfaceLog);
        }
        return "";
    }


    public static void main(String[] args) throws Exception {

//        String send = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\r\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n  <soap:Body>\r\n    <Get_Info_FrmbarcodeResponse xmlns=\"http://Auto.huawei.com.cn/\">\r\n      <sExport>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;!--Current Version:V03R15--&gt;&lt;GetEntityRevision&gt;&lt;Export&gt;&lt;ResultData&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05018EKV&lt;/SoftItem&gt;&lt;SoftName&gt;ONTPLUGINXML888&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC150A2205140037&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;1&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;50085334&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05018EKN&lt;/SoftItem&gt;&lt;SoftName&gt;HG8021PreconfigVA60&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC160A2206270076&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;1&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;50085334&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05024FWK&lt;/SoftItem&gt;&lt;SoftName&gt;HG8021VA34&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC166&lt;/Revision&gt;&lt;SoftLocation&gt;U1001&lt;/SoftLocation&gt;&lt;ModeProcduction&gt;0&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;2&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;0302027496&lt;/Segment4&gt;&lt;Segment5&gt;40060909&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05022TEM&lt;/SoftItem&gt;&lt;SoftName&gt;ONTXML217&lt;/SoftName&gt;&lt;Revision&gt;V200R019C30B040&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;2&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;0302027496&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05018UXR&lt;/SoftItem&gt;&lt;SoftName&gt;HG8021APPVA61&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC150&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;1&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;50085334&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;LineRow&gt;&lt;Zorgid&gt;157&lt;/Zorgid&gt;&lt;EntityName&gt;JPZQTR6168V&lt;/EntityName&gt;&lt;WorkNumber&gt;JPZQTR6168V&lt;/WorkNumber&gt;&lt;ItemNumber&gt;50085334&lt;/ItemNumber&gt;&lt;ItemVer&gt;0&lt;/ItemVer&gt;&lt;SoftItem&gt;05018NKK&lt;/SoftItem&gt;&lt;SoftName&gt;HG8021VAEfuse27&lt;/SoftName&gt;&lt;Revision&gt;V500R021C00SPC125&lt;/Revision&gt;&lt;SoftLocation/&gt;&lt;ModeProcduction&gt;1&lt;/ModeProcduction&gt;&lt;SpecialRevision&gt;0&lt;/SpecialRevision&gt;&lt;SoftLevel&gt;1&lt;/SoftLevel&gt;&lt;ExistUpdated&gt;N&lt;/ExistUpdated&gt;&lt;CreateBy&gt;999999999998950&lt;/CreateBy&gt;&lt;CreateDate&gt;2023/06/14 18:10:07&lt;/CreateDate&gt;&lt;UpdateBy&gt;999999999998950&lt;/UpdateBy&gt;&lt;UpdateDate&gt;2023/06/14 18:10:07&lt;/UpdateDate&gt;&lt;Segment1&gt;&lt;/Segment1&gt;&lt;Segment2&gt;&lt;/Segment2&gt;&lt;Segment3&gt;&lt;/Segment3&gt;&lt;Segment4&gt;50085334&lt;/Segment4&gt;&lt;Segment5&gt;&lt;/Segment5&gt;&lt;/LineRow&gt;&lt;/ResultData&gt;&lt;Message&gt;&lt;ErrorCode&gt;0&lt;/ErrorCode&gt;&lt;ErrorMsg&gt;Success&lt;/ErrorMsg&gt;&lt;/Message&gt;&lt;/Export&gt;&lt;/GetEntityRevision&gt;</sExport>\r\n    </Get_Info_FrmbarcodeResponse>\r\n  </soap:Body>\r\n</soap:Envelope>\r\n";
//        JSONObject object = XML.toJSONObject(send);
//        Map map3 = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class);
//        Map map4 = (Map) map3.get("soap:Envelope");
//        Map map5 = (Map) map4.get("soap:Body");
//        Map map6 = (Map) map5.get("Get_Info_FrmbarcodeResponse");
//        String sExport = (String) map6.get("sExport");
//
//        //将给定的String文本解析为XML文档并返回新创建的document
//        org.dom4j.Document document = DocumentHelper.parseText(sExport);
//        //获取根节点,在例子中就是responsedata节点
//        Element rootElement = document.getRootElement();
////        //获取根节点下的某个元素
//        Element Export = rootElement.element("Export");
//        Element ResultData = Export.element("ResultData");
//
//        Element Message = ResultData.element("Message");
//        Element ErrorCode = Message.element("ErrorCode");
//        Element ErrorMsg = Message.element("ErrorMsg");
//
//
//        //getData返回元素的数据
////        String resultcodeData = (String) Export.getData();
////        String data = (String) ResultData.getData();
////        //遍历所有子节点
////        for (Iterator i = LineRow.elementIterator(); i.hasNext(); ){
////            Element  next = (Element) i.next();
////            System.out.println(next.getName()+"："+(String) next.getData());
////        }
//        List<EntityRevisionDTO> list = new ArrayList<>();
//        //遍历某个子节点，如resultcode
//        for (Iterator i = ResultData.elementIterator("LineRow"); i.hasNext(); ){
//            EntityRevisionDTO entityRevision = new EntityRevisionDTO();
//            Element  next = (Element) i.next();
//            entityRevision.setZorgid((String) next.element("Zorgid").getData());
//            entityRevision.setEntityName((String) next.element("EntityName").getData());
//            entityRevision.setWorkNumber((String) next.element("WorkNumber").getData());
//            entityRevision.setItemNumber((String) next.element("ItemNumber").getData());
//            entityRevision.setItemVer((String) next.element("ItemVer").getData());
//            entityRevision.setSoftItem((String) next.element("SoftItem").getData());
//            entityRevision.setSoftName((String) next.element("SoftName").getData());
//            entityRevision.setRevision((String) next.element("Revision").getData());
//            entityRevision.setSoftLocation((String) next.element("SoftLocation").getData());
//            entityRevision.setModeProcduction((String) next.element("ModeProcduction").getData());
//            entityRevision.setSpecialRevision((String) next.element("SpecialRevision").getData());
//            entityRevision.setSoftLevel((String) next.element("SoftLevel").getData());
//            entityRevision.setExistUpdated((String) next.element("ExistUpdated").getData());
//            entityRevision.setCreateBy((String) next.element("CreateBy").getData());
//            entityRevision.setCreateDate((String) next.element("CreateDate").getData());
//            entityRevision.setUpdateBy((String) next.element("UpdateBy").getData());
//            entityRevision.setUpdateDate((String) next.element("UpdateDate").getData());
//            entityRevision.setSegment1((String) next.element("Segment1").getData());
//            entityRevision.setSegment2((String) next.element("Segment2").getData());
//            entityRevision.setSegment3((String) next.element("Segment3").getData());
//            entityRevision.setSegment4((String) next.element("Segment4").getData());
//            entityRevision.setSegment5((String) next.element("Segment5").getData());
//            list.add(entityRevision);
//        }
//
//        System.out.println(JSONUtil.toJsonStr(list));

    }



}
